//
//  pauseLayer.m
//  pipi
//
//  Created by zhao ys on 5/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "pauseLayer.h"
#import "HelloWorldLayer.h"
#import "SoundManager.h"
#import "GameLogic.h"
#import "settingLayer.h"
#import "playLayer.h"
#import "GameLogic.h"

@implementation pauseLayer

/*
+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	CCLayer *layer = [pauseLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}
*/

-(CCMenuItem *) CreateMenu:(NSString*)label target:(id)target selector:(SEL)selector {
	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_unprass.png"]];
	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_onprass.png"]];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:button_select target:target selector:selector];
	CCLabelTTF * l = [CCLabelTTF labelWithString:label fontName:@"Marker Felt" fontSize:32]; 
	l.color = ccc3(243, 189, 69);
	CGSize bSize = button.contentSize;
	l.position = ccp(bSize.width / 2, bSize.height / 2 + 5);
	[button addChild:l];
	return button;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        [[GameLogic sharedGameLogic] save];
        
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"dkz.plist"];

		
		//load back ground
		CCSpriteFrame * bgframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_startmenu_bg.png"];
		//CCSprite * bg = [CCSprite spriteWithSpriteFrame:bgframe];
		//bg.position = ccp(size.width / 2, size.height / 2);
		//[self addChild:bg z:-1];
		
		//label
		CCSprite * label = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_pause_001.png"]];
		label.position = ccp(size.width / 2, 678);
		label.visible = NO;
		[self addChild:label z:0 tag:4];

		
		CGRect rect1 = bgframe.rect;
		CGRect rect2 = bgframe.rect;
		rect1.size.width /= 2;
		rect2.origin.x += rect2.size.width / 2;
		
		CCSprite * bg1 = [CCSprite spriteWithTexture:bgframe.texture rect:rect1];
		CCSprite * bg2 = [CCSprite spriteWithTexture:bgframe.texture rect:rect2];
		
//		CGSize bg1Size = bg1.contentSize;
//		CGSize bg2Size = bg2.contentSize;
		bg1.position = ccp(-bg1.contentSize.width / 2, size.height / 2);
		bg2.position = ccp(size.width + bg2.contentSize.width / 2, size.height / 2);
		

		[self addChild:bg1 z:-1 tag:1];
		[self addChild:bg2 z:-1 tag:2];
		
		float delay = 0.3;
		id moveTo = [CCMoveTo actionWithDuration:delay position:ccp(bg1.contentSize.width/2, size.height/2)];
		id callFunc = [CCCallFunc actionWithTarget:self selector:@selector(OnPauseActionDone)];
		
		[bg1 runAction:[CCSequence actions:moveTo, callFunc, nil]];
		[bg2 runAction:[CCMoveTo actionWithDuration:delay position:ccp(size.width / 2 + bg2.contentSize.width/2, size.height/2)]];		

		
		//trinket
		CCSprite * trinket = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_002.png"]];
		trinket.position = ccp(180, 570);
		[bg1 addChild:trinket];
		
		
//		CCSprite * bg = [CCSprite spriteWithSpriteFrame:bgframe];
//		bg.position = ccp(size.width / 2, size.height / 2);
//		[self addChild:bg z:-1];
		CCMenuItem * resume = [self CreateMenu:@"resume" target:self selector:@selector(OnResume)];
		CCMenuItem * restart = [self CreateMenu:@"restart" target:self selector:@selector(OnRestart)];
		CCMenuItem * setting = [self CreateMenu:@"option" target:self selector:@selector(OnMenuSetting)];
		CCMenuItem * mainmenu = [self CreateMenu: @"main menu" target:self selector:@selector(OnMainMenu)];
		
		resume.position = ccp(0, 154);
		restart.position = ccp(0, 52);
		setting.position = ccp(0, -52);
		mainmenu.position = ccp(0, -154);
		CCMenu * menu = [CCMenu menuWithItems:resume, restart, setting, mainmenu, nil];
		//[menu alignItemsVerticallyWithPadding:0];
		menu.position = ccp(size.width / 2,size.height / 2 - 30);
		menu.visible = NO;
		[self addChild: menu z:0 tag:3];
		
		[[SoundManager sharedManager] stop:@"background_menu"];
		[[SoundManager sharedManager] stop:@"background"];
	}
	return self;
}

-(void)OnPauseActionDone {
	CCNode * menu = [self getChildByTag:3];
	menu.visible = YES;
	CCNode * label = [self getChildByTag:4];
	label.visible = YES;
}


-(void)OnResumeActionDone {
	[[CCDirector sharedDirector] popScene];
	//[[CCDirector sharedDirector] resume];
}


-(void) OnResume {
    [[GameLogic sharedGameLogic] load];
    
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	CGSize size = [[CCDirector sharedDirector] winSize];

	CCNode * menu = [self getChildByTag:3];
	menu.visible = NO;
	
	CCNode * label = [self getChildByTag:4];
	label.visible = NO;
	
	CCNode * bg1 = [self getChildByTag:1];
	CCNode * bg2 = [self getChildByTag:2];
	
	float delay = 0.5;
	id moveTo = [CCMoveTo actionWithDuration:delay position:ccp(-bg1.contentSize.width / 2, size.height / 2)];
	id callFunc = [CCCallFunc actionWithTarget:self selector:@selector(OnResumeActionDone)];
	
	[bg1 runAction:[CCSequence actions:moveTo, callFunc, nil]];
	[bg2 runAction:[CCMoveTo actionWithDuration:delay position:ccp(size.width + bg2.contentSize.width / 2, size.height / 2)]];
}

-(void) OnRestart {
	if (sound_on) [[SoundManager sharedManager] play:@"click" loops:1];
	
	[[CCDirector sharedDirector] popScene];

	[[GameLogic sharedGameLogic] Restart];
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
}

-(void) OnMenuSetting {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
	
	[[CCDirector sharedDirector] pushScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[settingLayer scene]]];	
}

-(void) OnMainMenu {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	[[CCDirector sharedDirector] popScene];
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:1.0f scene:[HelloWorldLayer scene]]];
	//[[CCDirector sharedDirector] resume];
}

@end
